package com.zhuss.core.processor;

import java.io.File;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.file.DeleteFileBo;
import com.zhuss.bean.bo.core.upload.DeleteUploadBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 删除文件 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 21:55
 */
@Component("uploadDeleteProcessor")
public class UploadDeleteProcessor extends AbstractProcessor<Object, DeleteUploadBo> implements Processor<Object, DeleteUploadBo> {

    @Autowired
	@Qualifier("fileDelete")
	private Processor<Object, DeleteFileBo> fileDelete;
    
    @Value("${filePath}")
    private String filePath;
    
    /**
     * 校验
     */
	@Override
    public void check(DeleteUploadBo bo) throws AabException {
		
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteUploadBo param) throws AabException {
    	String key = param.getKey();
    	
    	DeleteFileBo bo = new DeleteFileBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setFileKey(key);
    	fileDelete.process(bo);
    	param.setName(key);
    	
    	File file = new File(filePath + key);
        if (file.exists()) {
            file.delete();
        }
    	
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteUploadBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.文件删除.key());
    	record.setTargetId(null);
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
